package com.rajaev.dtu.tractor.domain.repository.mysql;


import com.rajaev.dtu.tractor.domain.entity.BatteryPo;
import com.rajaev.dtu.tractor.domain.entity.BatteryRunInfoResult;
import com.rajaev.dtu.tractor.domain.vo.BatteryAllocationQueryVo;
import com.rajaev.dtu.tractor.domain.vo.BatteryBasicVo;
import com.rajaev.dtu.tractor.domain.vo.BatteryMapVo;
import com.rajaev.dtu.tractor.domain.vo.BatteryQueryVo;
import com.rajaev.dtu.tractor.infrastructure.base.enums.BatteryOnlineStatus;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Mapper
@Repository
public interface BatteryDao {


    @Select({
            "select count(1) from t_battery where vin = #{vin}"
    })
    Integer countByVin(String vin);

    @Select("select * from t_battery where terminal_id  = #{terminalId}")
    @ResultMap("BatteryPoMap")
    BatteryPo selectBatteryByTerminalId(@Param("terminalId") String terminalId);

    @Select({
            "select vin from t_battery where vin like concat('%',#{keyword},'%')"
    })
    List<String> selectVins(String keyword);


    @Select("select * from t_battery")
    @ResultMap("BatteryPoMap")
    List<BatteryPo> selectAllBattery();


    List<BatteryMapVo> selectAllBatteryByCustomerId(@Param("customerIds") List<Integer> customerIds);


    @Select("select * from t_battery where create_datetime<=#{dataDatetime}")
    @ResultMap("BatteryPoMap")
    List<BatteryPo> selectAllBatteryByCreateTime(@Param("dataDatetime") Date dataDatetime);

    /**
     * 查询电池总数
     * @return
     */
    @Select("select count(1) from t_battery ")
    Integer countAll();

    @Select("select count(1) from t_battery where customer_id in (select customer_id from t_user_customer where user_id = #{userId})")
    Integer countAllByCuLoginUserId(Integer userId);

    @Select("select count(1) from t_battery where state = '1' ")
    Integer countPassed();

    /**
     * 标定电池的vin
     * @param batteryPo
     * @return
     */
    @Insert({
            "insert into t_battery (vin,create_datetime,state,customer_id) value (#{vin},sysdate(),'0',#{customerId})"
    })
    @Options(useGeneratedKeys=true, keyColumn="id")
    int saveBatteryVin(BatteryPo batteryPo);

    @Update("update t_battery set did = #{did} where id = #{batteryId}")
    void updateBatterDid(@Param("batteryId") Integer batteryId,@Param("did") String did);


    @Update("update t_battery set tbox_number = #{newTboxNumber} where id = #{batteryId}")
    int updateTboxNumber(@Param("batteryId") Integer batteryId,
                     @Param("newTboxNumber") String newTboxNumber);

    @Update("update t_battery set terminal_id = #{terminalId} where id = #{batteryId}")
    int updateTerminalId(@Param("batteryId") Integer batteryId,
                         @Param("terminalId") String terminalId);

    int updateBatteryRunInfo(BatteryRunInfoResult batteryRunInfoResult);


    @Update("update t_battery set bz = #{bz},plate_no= #{plateNo},device_manager = #{deviceManager} where id = #{batteryId}")
    void updateBatteryInfo(@Param("batteryId") Integer batteryId,
                           @Param("bz") String bz,
                           @Param("plateNo") String plateNo,@Param("deviceManager") String deviceManager);

    @Update("update t_battery set battery_cell_num = #{cellNum},battery_terperature_num= #{temperatureNum} where id = #{batteryId}")
    void updateBatteryCellNumAndTemperatureNum(@Param("batteryId") Integer batteryId,
                                              @Param("temperatureNum")  Integer temperatureNum,
                                              @Param("cellNum")   Integer cellNum);


    @Update("update t_battery set project_team_id = #{projectTeamId} where id = #{batteryId}")
    void updateBatteryProjectTeam(@Param("batteryId") Integer batteryId,
                                  @Param("projectTeamId") Integer projectTeamId);

    /**
     * 根据vin删除标定信息
     * @param vin
     */
    @Delete({
            "delete from t_battery where vin = #{vin}"
    })
    void deleteByVin(String vin);


    List<BatteryPo> selectByLastVin4(@Param("vin4") String vin4);

    @Select("select * from t_battery where customer_id = #{customerId}")
    @ResultMap("BatteryPoMap")
    List<BatteryPo> selectByCustomerId(Integer customerId);

    List<BatteryPo> selectByCustomerIds(@Param("customerIds") List<String > customerIds);


    List<BatteryBasicVo> selectByBatterQueryVo(BatteryQueryVo batteryQueryVo);

    /**
     *
     * @param start
     * @param pagesize
     * @param belongUserId 所属用户的id
     * @return
     */
    List<BatteryPo> selectBattery(@Param("start") long start,@Param("pagesize") int pagesize,@Param("userId")  Integer belongUserId);

    List<BatteryPo> selectUserBattery(@Param("userId")  Integer belongUserId);

    List<BatteryPo> selectBatteryInFault(@Param("userId")  Integer belongUserId);


    List<BatteryBasicVo> selectByBatteryAllocateInfo(BatteryAllocationQueryVo batteryAllocationQueryVo);

    @Update({
            "update t_battery set customer_id = #{customerId} where id = #{batteryId}"
    })
    void updateBatteryCustomer(@Param("batteryId") Integer batteryId,
                               @Param("customerId") Integer customerId);

    BatteryPo selectById(Integer id);

    BatteryPo selectByDid(String  did);


    void updateBatteryAllowCharge(@Param("vin") String vin,@Param("allowStatus")  String allowStatus);



    BatteryPo selectByVin(String vin);


    @Update({
            "update t_battery set state = #{state},check_time= CURRENT_TIMESTAMP where id = #{batteryId} "
    })
    int updateState(@Param("batteryId") Integer batteryId,@Param("state") String state);

    void  updateBatteryStatus(@Param("batteryId") Integer batteryId,@Param("onlineStatus") BatteryOnlineStatus onlineStatus);


}
